package tech.anzhen.simple.rpc.support.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import tech.anzhen.simple.rpc.support.rest.RestController;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author: Anzhen
 **/
@Slf4j
@Configuration
public class RpcSupportAutoConfiguration implements BeanFactoryAware {

    private ListableBeanFactory beanFactory;

    @PostConstruct
    public void init() {
        log.info("开始加载RPC配置");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        if (beanFactory instanceof ListableBeanFactory) {
            this.beanFactory = (ListableBeanFactory) beanFactory;
        } else {
            log.info("RpcSupportAutoConfiguration not running in a ListableBeanFactory: autodetection not available.");
        }
    }

    @ConditionalOnMissingBean(RestController.class)
    @Bean
    protected RestController restController(ListableBeanFactory beanFactory) {
        return new RestController(beanFactory) {};
    }
}
